home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / unarced / graphics / anim / packer.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  3KB  |  112 lines

  1. /*----------------------------------------------------------------------*
  2.  * packer.c Convert data to "cmpByteRun1" run compression.     11/15/85
  3.  *
  4.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  5.  * This software is in the public domain.
  6.  *
  7.  *    control bytes:
  8.  *     [0..127]   : followed by n+1 bytes of data.
  9.  *     [-1..-127] : followed by byte to be repeated (-n)+1 times.
  10.  *     -128       : NOOP.
  11.  *
  12.  * This version for the Commodore-Amiga computer.
  13.  *----------------------------------------------------------------------*/
  14. #include "functions.h"
  15. #include "packer.h"
  16.  
  17. #define DUMP    0
  18. #define RUN    1
  19.  
  20. #define MinRun 3    
  21. #define MaxRun 128
  22. #define MaxDat 128
  23.  
  24. LONG putSize;
  25. #define GetByte()    (*source++)
  26. #define PutByte(c)    { *dest++ = (c);   ++putSize; }
  27.  
  28. char buf[256];    /* [TBD] should be 128?  on stack?*/
  29.  
  30. BYTE *PutDump(dest, nn)  BYTE *dest;  long nn; {
  31.     long i;
  32.  
  33.     PutByte(nn-1);
  34.     for(i = 0;  i < nn;  i++)   PutByte(buf[i]);
  35.     return(dest);
  36.     }
  37.  
  38. BYTE *PutRun(dest, nn, cc)   BYTE *dest;  long nn, cc; {
  39.     PutByte(-(nn-1));
  40.     PutByte(cc);
  41.     return(dest);
  42.     }
  43.  
  44. #define OutDump(nn)   dest = PutDump(dest, nn)
  45. #define OutRun(nn,cc) dest = PutRun(dest, nn, cc)
  46.  
  47. /*----------- PackRow --------------------------------------------------*/
  48. /* Given POINTERS TO POINTERS, packs one row, updating the source and
  49.    destination pointers.  RETURNs count of packed bytes.*/
  50. LONG PackRow(pSource, pDest, rowSize)
  51.     BYTE **pSource, **pDest;   LONG rowSize; {
  52.     BYTE *source, *dest;
  53.     char c,lastc = '\0';
  54.     BOOL mode = DUMP;
  55.     short nbuf = 0;        /* number of chars in buffer */
  56.     short rstart = 0;        /* buffer index current run starts */
  57.  
  58.     source = *pSource;
  59.     dest = *pDest;
  60.     putSize = 0;
  61.     buf[0] = lastc = c = GetByte();  /* so have valid lastc */
  62.     nbuf = 1;   rowSize--;    /* since one byte eaten.*/
  63.  
  64.  
  65.     for (;  rowSize;  --rowSize) {
  66.     buf[nbuf++] = c = GetByte();
  67.     switch (mode) {
  68.         case DUMP: 
  69.             /* If the buffer is full, write the length byte,
  70.                then the data */
  71.             if (nbuf>MaxDat) {
  72.                 OutDump((long)(nbuf-1));  
  73.                 buf[0] = c; 
  74.                 nbuf = 1;   rstart = 0; 
  75.                 break;
  76.                 }
  77.  
  78.             if (c == lastc) {
  79.                 if (nbuf-rstart >= MinRun) {
  80.                 if (rstart > 0) OutDump((long)rstart);
  81.                 mode = RUN;
  82.                 }
  83.                 else if (rstart == 0)
  84.                 mode = RUN;    /* no dump in progress,
  85.                 so can't lose by making these 2 a run.*/
  86.                 }
  87.             else  rstart = nbuf-1;        /* first of run */ 
  88.             break;
  89.  
  90.         case RUN: if ( (c != lastc)|| ( nbuf-rstart > MaxRun)) {
  91.                 /* output run */
  92.                OutRun((long)(nbuf-1-rstart),(long)lastc);
  93.                 buf[0] = c;
  94.                 nbuf = 1; rstart = 0;
  95.                 mode = DUMP;
  96.                 }
  97.             break;
  98.         }
  99.  
  100.     lastc = c;
  101.     }
  102.  
  103.     switch (mode) {
  104.     case DUMP: OutDump((long)nbuf); break;
  105.     case RUN: OutRun((long)(nbuf-rstart),(long)lastc); break;
  106.     }
  107.     *pSource = source;
  108.     *pDest = dest;
  109.     return(putSize);
  110.     }
  111.  
  112.